<?php

namespace App\Console\Commands;

use App\Models\TaskTemplate;
use App\Services\TaskService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class PublishTasks extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:publish-tasks {day?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $day = $this->argument('day');
        $templates = [];
        if (date('d') == '09' || $day == 9) {
            $templates = TaskTemplate::where('sheet_template_id', '=', 114)
                ->where([
                    'frequency' => 'monthly',
                    'status' => TaskTemplate::STATUS_ON,
                ])
                ->get();
        } elseif (date('d') == '17' || $day == 17) {
            $templates = TaskTemplate::where('sheet_template_id', '!=', 114)
                ->where([
                    'frequency' => 'monthly',
                    'status' => TaskTemplate::STATUS_ON,
                ])
                ->get();
        }

        foreach ($templates as $template) {
            try {
                TaskService::create($template);
                Log::info('发布任务模板: ' . $template->name);
            } catch (\Throwable $th) {
                Log::info('发布任务模板异常: ' . $template->name . ' ' . $th->getMessage());
                continue;
            }
        }
    }
}
